import { ElMessage } from 'element-plus';
import { useMemberMoneyApi } from '/@/api/member';

import { deepCopy } from '/@/utils';

const { getMoneyList, updateMoney, addMoney } = useMemberMoneyApi();

export const useInit = () => {
	const isShowDialog = ref(false);
	const detailForm = ref({});
	const tableData = ref([]);
	const formData = ref({
		add: '',
		moneyStatus: '',
		project: '',
		startTime: null,
		sussTime: null,
	});
	const page = () => {
		return {
			current: 1,
			pageSize: 10,
			total: 0,
		};
	};

	const ipage = ref(page());

	const handleGetMoneyList = async () => {
		let res = await getMoneyList(ipage.value);
		if (res.code === 200) {
			let { data, total } = res.data;
			tableData.value = data.map((item) => {
				item.add = item.addSub || 0;
				item.createTime = item.createTime || '';
				item.current = item.current || 0;
				item.email = item.email || '';
				item.endTime = item.endTime || '';
				item.noteTime = item.noteTime || '';
				item.id = item.id || '';
				item.moneyStatus = item.moneyStatus || 0;
				item.note = item.note || '';
				item.pageSize = item.pageSize || 0;
				item.project = item.project || '';
				item.startTime = item.startTime || '';
				item.updateTime = item.updateTime || '';
				item.sussTime = item.sussTime || '';
				item.userId = item.userId || 0;
				return item;
			});
			ipage.value.total = total;
		}
	};

	const onOpenDialog = (item) => {
		let data = deepCopy(item);
		console.log('00=0=10=', data);
		// data.createTime = timeFormat(data.createTime);
		// data.startTime = timeFormat(data.startTime);
		// data.updateTime = timeFormat(data.updateTime);
		// data.endTime = timeFormat(data.endTime);
		// data.addSub = getStateFormatZh(extractType, data.addSub);
		// data.project = getStateFormatZh(extractProject, data.project);
		// data.moneyStatus = getStateFormatZh(extractMoneyStatus, data.moneyStatus);
		detailForm.value = data;
		console.log(detailForm.value);
		isShowDialog.value = true;
	};

	const onCancel = () => {
		isShowDialog.value = false;
	};
	const onSubmit = () => {
		isShowDialog.value = false;
	};

	return {
		ipage,
		tableData,
		formData,
		isShowDialog,
		detailForm,
		handleGetMoneyList,
		onOpenDialog,
		onCancel,
		onSubmit,
	};
};

export const useAdd = () => {
	const formData = ref();
	const isShowAddDialog = ref(false);
	const onAdd = () => {
		isShowAddDialog.value = true;
	};
	const onCancelAdd = () => {
		isShowAddDialog.value = false;
	};

	const handleAdd = async (fromRef) => {
		console.log(fromRef);
		// let res = await addMoney(formData);
	};
	const onSubmitAdd = async (addEl) => {
		const { ruleFormRef, formData } = addEl;
		console.log('0-0--', ruleFormRef);
		if (!ruleFormRef) return;
		ruleFormRef.validate(async (valid) => {
			if (valid) {
				try {
					if (!formData.add || !formData.project || !formData.moneyStatus) {
						ElMessage.error('类型或项目或状态不能为全部！');
						return;
					}
					let res = await addMoney(formData);
					if (res.code == 200) {
						ElMessage.success('添加成功！');
						isShowAddDialog.value = false;
					}
				} catch (err) {
					ElMessage.error(`添加失败！`);
				}
			}
		});
	};

	return {
		formData,
		isShowAddDialog,
		onAdd,
		onCancelAdd,
		handleAdd,
		onSubmitAdd,
	};
};

export const useEdit = (handleGetMoneyList) => {
	const updateRules = {
		note: [{ required: true, trigger: 'blur', message: '请输入备注' }],
	};
	const updateAudit = ref({});
	const info = ref();
	const isShowAuditDialog = ref(false);

	const onOpenAuditModal = (item) => {
		info.value = item;
		isShowAuditDialog.value = true;
	};

	const onCloseAndit = () => {
		isShowAuditDialog.value = false;
	};

	const onSubmitudit = (option) => {
		updateAudit.value.moneyStatus = option.state;
		updateAudit.value.id = info.value.id;
		option.updateFormRef.validate(async (valid) => {
			if (valid) {
				let res = await updateMoney(updateAudit.value);
				if (res.code === 200) {
					ElMessage({
						type: 'success',
						message: '保存成功',
					});
					handleGetMoneyList();
					onCloseAndit();
				}
			}
		});
	};

	return {
		isShowAuditDialog,
		updateAudit,
		updateRules,
		onOpenAuditModal,
		onCloseAndit,
		onSubmitudit,
	};
};
